package com.pachong.android.frameworkbase.customviews;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.pachong.android.frameworkbase.R;

import java.util.ArrayList;
import java.util.List;


/**
 * 直接刷ImageView的控件
 * <p>
 * 通过配置url就可以直接使用
 *
 * @author Vivi Peng
 */
public class ImageViewPager extends ViewPager {

    protected ImagesViewPagerAdapter mAdapter;

    public ImageViewPager(Context context) {
        super(context);
    }


    public ImageViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    public void setupUrls(List<String> urls) {
        mAdapter = createAdapter();
        mAdapter.setupUrls(urls);
        setAdapter(mAdapter);
    }

    public void setupUrls(String[] urls) {
        List<String> list = new ArrayList<>();
        for (String url : urls) {
            list.add(url);
        }
        setupUrls(list);
    }

    public void setupDrawableIds(List<Integer> resIds) {
        mAdapter = createAdapter();
        mAdapter.setupDrawableIds(resIds);
        setAdapter(mAdapter);
    }

    public void setupDrawableIds(int[] resIds) {
        List<Integer> list = new ArrayList<>();
        for (int resId : resIds) {
            list.add(resId);
        }
        setupDrawableIds(list);
    }

    protected ImagesViewPagerAdapter createAdapter() {
        return new ImagesViewPagerAdapter(getContext());
    }


    public class ImagesViewPagerAdapter extends PagerAdapter {

        private List<FrameLayout> mImageParents = new ArrayList<>();
        private List<String> mUrls = new ArrayList<String>();
        private List<Integer> mDrawableIds = new ArrayList<>();
        private int mCount = 0;
        Context mContext;

        public ImagesViewPagerAdapter(Context context) {
            this.mContext = context;
        }


        private void setupDrawableIds(List<Integer> resIds) {
            if (resIds != null && resIds.size() > 0) {
                mDrawableIds.clear();
                mDrawableIds = resIds;
                for (int i = 0; i < mDrawableIds.size(); i++) {
                    FrameLayout imgParent = new FrameLayout(mContext);
                    mImageParents.add(imgParent);
                }
                mCount = mDrawableIds.size();
                notifyDataSetChanged();
            }
        }


        public void setupUrls(List<String> urls) {
            if (urls != null && urls.size() > 0) {
                mUrls.clear();
                mUrls = urls;
                for (int i = 0; i < mUrls.size(); i++) {
                    FrameLayout imgParent = new FrameLayout(mContext);
                    mImageParents.add(imgParent);
                }
                mCount = mUrls.size();
                notifyDataSetChanged();
            }
        }


        protected ImageView createNewImageView() {
            ImageView imgView = new ImageView(mContext);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
            imgView.setLayoutParams(params);
            imgView.setImageResource(R.drawable.ic_launcher);
            imgView.setScaleType(ScaleType.CENTER_CROP);
            return imgView;
        }


        @Override
        public int getCount() {
            return mCount;
        }


        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }


        @Override
        public void destroyItem(View container, int position, Object object) {
            int index = position % mImageParents.size();
            mImageParents.get(index).removeAllViews();
        }


        /**
         * 载入图片进去，用当前的position 除以 图片数组长度取余数是关键
         */
        @Override
        public Object instantiateItem(View container, final int position) {
            int index = position % mImageParents.size();
            final FrameLayout imgParent = mImageParents.get(index);
            final ImageView imgView = createNewImageView();
            imgParent.addView(imgView);
            imgView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onItemClick(null, imgView, position, -1);
                    }

                }
            });

            try {
                ((ViewPager) container).addView(imgParent, 0);
            } catch (Exception e) {
            }

            setImage(imgView, position);
            return imgParent;
        }

        private void setImage(final ImageView imgView, int position) {
            if (mUrls != null && mUrls.size() > 0) {
                com.pachong.android.frameworkbase.imageloader.ImageLoader.with(getContext()).load(mUrls.get(position)).into(imgView).start();

            } else if (mDrawableIds != null && mDrawableIds.size() > 0) {
                imgView.setImageResource(mDrawableIds.get(position));
            }
        }

    }

    OnItemClickListener mListener;


    public void setOnItemClickedListenner(OnItemClickListener listener) {
        mListener = listener;
    }

}
